package com.cloudfast.platform.role.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.page.Page;
import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.context.error.CloudException;
import com.cloudfast.context.error.ErrorCode;
import com.cloudfast.controller.BaseController;
import com.cloudfast.platform.role.entity.Role;
import com.cloudfast.platform.role.service.RoleService;

/**
 * @author liuyw
 * @描述:
 * @date 2020年4月30日
 */
@RestController
@RequestMapping(value = "role")
@Api(value = "角色管理", tags = "角色管理")
public class RoleController extends BaseController<Role> {

    @Autowired
    private RoleService roleService;

    @Override
    public ApiResponse findOne(Long id) {
        try {
            return ApiResponse.success(this.roleService.findOne(id));
        } catch (Exception e) {
            throw new CloudException(ErrorCode.FIND_ONE_ERROR);
        }
    }

    @Override
    public ApiResponse findPage(Page<Role> page, Role entity) {
        try {
            return ApiResponse.success(this.roleService.findPage(page, entity));
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new CloudException(ErrorCode.FIND_PAGE_ERROR);
        }
    }

    @Override
    public ApiResponse save(Role entity) {
        try {
            roleService.save(entity);
            return ApiResponse.success("操作成功");
        } catch (Exception e) {
            logger.error(e.getMessage());

            throw new CloudException(ErrorCode.SAVE_ERROR);
        }
    }

    @Override
    public ApiResponse delete(Long id) {
        try {
            if (!roleService.hasUser(id)) {
                roleService.delete(id);
                return ApiResponse.success("");
            } else {
                return ApiResponse.error(ErrorCode.DELETE_ROLE_HAS_USER.getMsg());
            }
        } catch (Exception e) {
            throw new CloudException(ErrorCode.DELETE_ERROR);
        }
    }

    @Override
    public ApiResponse findList(Role entity) {
        try {
            return ApiResponse.success(roleService.findList(entity));

        } catch (Exception e) {
            throw new CloudException(ErrorCode.SYSTEM_ERROR);

        }
    }

    /**
     * @param id
     * @param code
     * @return
     * @描述:监测编码是否存在
     * @author liuyw
     * @date 2020年4月20日
     */
    @PostMapping(value = "checkCode")
    @ApiOperation("检测角色编码是否存在")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "code", value = "角色编码", required = true)})
    public ApiResponse checkCode(Long id, String code) {
        try {
            return ApiResponse.success(roleService.checkCode(id, code));
        } catch (Exception e) {
            throw new CloudException(ErrorCode.SYSTEM_ERROR);
        }
    }

    @PostMapping(value = "grantUser")
    @ApiOperation("角色赋权")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "roleIds", value = "角色ids", required = true)})
    public ApiResponse grantUser(Long userId, String roleIds) {
        try {
            this.roleService.clearUserRoleByUserId(userId);
            String[] roles = roleIds.split(",");
            for (int i = 0; i < roles.length; i++) {
                Long roleId = Long.valueOf(roles[i]);
                this.roleService.addUser(roleId, userId);
            }
            return ApiResponse.success(true);
        } catch (Exception e) {
            throw new CloudException(ErrorCode.SYSTEM_ERROR);
        }
    }
}
